/**
 * @file json.c
 * @author yangqingjin
 * @brief
 * @version 0.1
 * @date 2022-12-20
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "json.h"

int read_json(char *buf)
{
    FILE *fp = fopen("/home/yqj/hq_plat/res/node.json", "r");
    if (NULL == fp)
    {
        perror("open error\n");
        exit(-1);
    }
    int ret = fread(buf, 2500, 1, fp);
    if (ret < 0)
    {
        printf("read file error\n");
        return -1;
    }
    return 0;
}
/**
 * @brief 打印出mb设备的连接信息
 * 
 * @param buf_file 
 * @return struct modbus_json* 
 */
struct modbus_json *read_modbus_json_part(char *buf_file)
{

    cJSON *root = NULL;
    cJSON *item = NULL;

    modbus_T *pmodbus = (modbus_T *)malloc(sizeof(modbus_T));
    const char *buf = buf_file;
    root = cJSON_Parse(buf);
     
    if (root == NULL)
    {
        printf("error before: [%s]\n", cJSON_GetErrorPtr());
        return NULL;
    }
    else
    {
        
        // 获取modbus的连接信息
        item = cJSON_GetObjectItem(root, "mb_dev");
        cJSON *addr = cJSON_GetObjectItem(item, "addr");
        strcpy(pmodbus->ip, addr->valuestring);
        printf("ip=%s\n", pmodbus->ip);

        cJSON *port = cJSON_GetObjectItem(item, "port");
        pmodbus->port = port->valueint;
        printf("port=%d\n", pmodbus->port);

        item = cJSON_GetObjectItem(root, "modbus");
        cJSON *name = cJSON_GetObjectItem(item, "name");
        sprintf(pmodbus->name, "%s", name->valuestring);
        printf("name=%s\n", pmodbus->name);
    }
    
    return pmodbus;
}


/**
 * @brief 做缓存链表
 * 
 * @param buf_file 
 * @param shm_addr 
 * @return struct node_list* 
 */
struct node_list *mb_node_list_init(char *buf_file, struct std_node *shm_addr)
{

    struct list_head headNode;
    INIT_LIST_HEAD(&headNode);
    char *buf = buf_file;
    struct node_list *mb_node = NULL;
    cJSON *root = NULL;
    cJSON *item = NULL;

    root = cJSON_Parse(buf);
    if (root == NULL)
    {
        printf("error before: [%s]\n", cJSON_GetErrorPtr());
        return NULL;
    }
    int arry_size;
    int i;
    cJSON *array = NULL;
    item = cJSON_GetObjectItem(root, "modbus");
    cJSON *data = cJSON_GetObjectItem(item, "data");
    arry_size = cJSON_GetArraySize(data);

    for (i = 0; i < arry_size; i++)
    {
        mb_node = (struct node_list *)malloc(sizeof(struct node_list));

        array = cJSON_GetArrayItem(data, i);

        cJSON *key = cJSON_GetObjectItem(array, "key");
        mb_node->dataDomain.key = key->valueint;

        cJSON *name = cJSON_GetObjectItem(array, "name");
        sprintf(mb_node->dataDomain.name, "%s", name->valuestring);

        cJSON *addr = cJSON_GetObjectItem(array, "addr");
        mb_node->dataDomain.addr = addr->valueint;

        cJSON *type = cJSON_GetObjectItem(array, "type");
        mb_node->dataDomain.type = type->valueint;

        list_add_tail(&mb_node->pointDomain, &headNode);
    }

    struct list_head *pos = NULL;
    struct node_list *tmpNode = NULL;

    list_for_each(pos, &headNode)
    {
        tmpNode = list_entry(pos, struct node_list, pointDomain);

        printf("key = %d, name = %s, addr = %d ,type = %d\n", tmpNode->dataDomain.key, tmpNode->dataDomain.name,\
              tmpNode->dataDomain.addr, tmpNode->dataDomain.type);
        shm_addr[tmpNode->dataDomain.key - 101].key = tmpNode->dataDomain.key;
        shm_addr[tmpNode->dataDomain.key - 101].type = tmpNode->dataDomain.type;
        shm_addr[tmpNode->dataDomain.key - 101].dev_type = 1;
        shm_addr[tmpNode->dataDomain.key - 101].ret = -1;
        
    }
    return mb_node;
}
